#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

// 内存写入磁盘中
int main()
{
    // 打开文件
    const char *filename = "./readme.md";
    int fd = open(filename, O_CREAT | O_RDWR, 0644);
    if (fd == -1)
    {
        perror("open error:");
        exit(-1);
    }
    // printf("fd = %d\n", fd);

    /*将文件的内容读取到内存中*/
    // char buffer[32] = {0};
    // int readBytes = read(fd,buffer,sizeof(buffer)-1);  // 留一个位置给'/0'
    // printf("readBytes = %d,\tbuffer = %s\n",readBytes,buffer);
    
    char buffer[128] = {0};
    int requestBytes = sizeof(buffer) - 1;
    while (1)
    {
        int readBytes = read(fd, buffer, requestBytes); // 不读"/0"
        if((readBytes < requestBytes) || readBytes == 0)
        {
            printf("%s\n",buffer);
            break;
        }
        printf("%s",buffer);
        // printf("readBytes = %d,\tbuffer = %s\n", readBytes, buffer);
        
        // 清楚缓冲区
        memset(buffer,0,sizeof(buffer));
    }

    close(fd);

    return 0;
}

// 命令行参数，可以实现文件的复制 
